package com.zhong.gang.app.ui.address;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zhong.gang.app.R;
import com.zhong.gang.app.api.HttpUtils;
import com.zhong.gang.app.ui.contract.ContractBean;
import com.zhong.gang.app.utils.AddressListener;
import com.zhong.gang.applib.base.act.ActBaseRefresh;
import com.zhong.gang.applib.config.Config;
import com.zhong.gang.applib.databinding.LayoutPullToBinding;
import com.zhong.gang.applib.http.BaseDataBean;
import com.zhong.gang.applib.modle.ListBean;
import java.util.ArrayList;

public class AddressAct extends ActBaseRefresh<LayoutPullToBinding,AddressAdapter> implements BaseQuickAdapter.OnItemClickListener {
    private static AddressListener listener;
    public static final void openAct(Context context, String url, AddressListener listener){
        Intent intent = new Intent(context,AddressAct.class);
        intent.putExtra(Config.URL,url);
        AddressAct.listener = listener;
        context.startActivity(intent);
    }
    @Override
    public int LayoutRes() {
        return R.layout.layout_pull_to;
    }

    @NonNull
    @Override
    public String CreateTitle() {
        return "请选择";
    }

    @Override
    public void initView() {
        bind.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        initrefrsh(bind.pullto,new AddressAdapter(),bind.recyclerView);
        adapter.setOnItemClickListener(this);
        getData();
    }

    @Override
    public void getData() {
        super.getData();
        HttpUtils.get(getIntent().getStringExtra(Config.URL),this)
                .addParams("current",page+"")
                .addParams("size","20")
                .build().execute(new ActCallBack<BaseDataBean<ListBean<AddressBean>>>(){
            @Override
            public void onResponse(BaseDataBean<ListBean<AddressBean>> data) throws Exception {
                super.onResponse(data);
                setData(data.getResult().getRecords());
            }

            @Override
            public void onError(String str) {
                super.onError(str);
                setData(new ArrayList<ContractBean.ContractMaterialTypesBean>());
            }
        });
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        AddressBean bean = (AddressBean) adapter.getItem(position);
        if (listener != null)
            listener.onAddressBack(bean);
        closeActivity();
    }
}
